//
//  YDBBaseViewController.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2018/8/2.
//  Copyright © 2018年 杨东宝. All rights reserved.
//

import UIKit

class YDBBaseViewController: UIViewController {
    
    // 用户信息
    var userInfo: NSDictionary?
    // 字典信息
    var dictionaryInfo: NSDictionary?
    // 数组信息
    var arrayInfo: NSArray?
    // 字符串信息
    var stringInfo: NSString?
    // 整型信息
    var integerInfo: NSInteger?
    // 当前控制器名称
    var ctrlName: NSString?
    // 前一控制器名称
    var preCtrlName: NSString?
    
    // MARK: - 懒加载TableView
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        tableView.estimatedRowHeight = 100.0
        tableView.rowHeight = UITableView.automaticDimension
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = .none;
        tableView.myh_header = MYHRefreshNormalHeader.init(refreshingBlock: {
            [weak self] in
            self?.setupLoadData()
        })
        tableView.myh_footer = MYHRefreshBackNormalFooter.init(refreshingBlock: {
            [weak self] in
            self?.setupLoadMoreData()
        })
        return tableView
    }()

    func setupLoadData(){}
    func setupLoadMoreData(){}
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = HexRGB(0xF3F3F3)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DebugLog("当前控制器："+self.className())
    }
    //MARK:-添加导航栏 - 文字按钮
    func addNavigationItems(titles:Array<String>,isLeft:Bool,target:(Any)?,action:(Selector),tags:Array<Int>){
        
        var items = Array<UIBarButtonItem>()
        
        for (index,title) in titles.enumerated() {
            
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
            btn.setTitle(title, for: .normal)
            btn.addTarget(target, action: action, for: .touchUpInside)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            btn.setTitleColor(hexColor(0x333333,0xA7A7B3), for: .normal)
            btn.tag = tags[index]
            btn.sizeToFit()
            if isLeft {
                btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 10)
            }else{
                btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: -10)
            }
            let item = UIBarButtonItem.init(customView: btn)
            items.append(item)
            
        }
        if isLeft {
            self.navigationItem.leftBarButtonItems = items
        }else{
            self.navigationItem.rightBarButtonItems = items
        }
    }
    //MARK:-添加导航栏 - 图片
    func addNavigationItemsWithImages(images:Array<String>,isLeft:Bool,target:(Any)?,action:(Selector),tags:Array<Int>){
        
        var items = Array<UIBarButtonItem>()
        
        for (_,title) in images.enumerated() {
            var btn: UIButton?
            if #available(iOS 11, *) {
                btn = UIButton(type: UIButton.ButtonType.custom)
                btn?.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
                btn?.setImage(imageNamed(title), for: UIControl.State.normal)
                btn?.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
                btn?.addTarget(self, action:action, for: UIControl.Event.touchUpInside)
                btn?.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
                btn?.imageEdgeInsets = UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 20)
    
            }
            else {
                btn = UIButton.init(frame: CGRect(x: 0, y: 0, width: 18, height: 18))
                btn?.setImage(imageNamed(title), for: UIControl.State.normal)
                btn?.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
                btn?.addTarget(self, action:action, for: UIControl.Event.touchUpInside)
            }
            let item = UIBarButtonItem.init(customView: btn!)
            items.append(item)
        }
        if isLeft {
            self.navigationItem.leftBarButtonItems = items
        }else{
            self.navigationItem.rightBarButtonItems = items
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension YDBBaseViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")!
        cell.selectionStyle = .none
        
        return cell
    }
    
    
}
